package cn.kginglam.dynamic_excel.common.utils;

import cn.hutool.core.util.StrUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author: Lam
 * @date: 2021-09-22 17:30
 * @description:
 */
public class BeanUtils {


    public static void removeFieldSpace(Object o) throws Exception {
        Class clazz = o.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getGenericType().toString().equals("class java.lang.String")) {
                Method getMethod = clazz.getMethod("get" + getMethodName(field.getName()));
                String value = (String) getMethod.invoke(o);// 调用getter方法获取属性值
                if (StrUtil.isNotBlank(value)) {
                    value = value.replaceAll("\\s*", "");
                    field.setAccessible(true);
                    field.set(o, value);
                }
            }
        }
    }


    /*
     * @Description: 把一个字符串的第一个字母大写
     */
    private static String getMethodName(String fieldName) throws Exception {
        byte[] items = fieldName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }
}
